package com.example.onion.hxchat.room.repositorywc;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import com.example.onion.hxchat.room.dao.InfoDao;
import com.example.onion.hxchat.room.entity.Info;
import com.example.onion.hxchat.room.wcdatabase.MyDataBaseWc;

import java.util.List;

/**
 * @author:onion
 * @data: 2020/7/8
 */
public class InfoRepository {
    /**
     * Data list from Dao.
     */
    private LiveData<List<Info>> infos;


    private InfoDao infoDao;

    private  MyDataBaseWc database;

    public InfoRepository(Context context) {
        //获取数据库实例，数据库采用单例模式
        database = MyDataBaseWc.getInstance(context.getApplicationContext());
        infoDao = database.getInfoDao();
        infos = infoDao.getAllInfo();
    }

    /**
     * Gets all data from Master table from database.
     * @return The list of users
     */
    public LiveData<List<Info>> getAllInfos() {
        return infos;
    }


    public LiveData<List<Info>> getUsersInfo(String userID)
    {
        return infoDao.getAllInfoCur(userID);
    }



    public void insertMaster(Info... infos){
        new InfoRepository.InsertMasterAsyncTask(infoDao).execute(infos);
    }

    /**
     * 传入lastInfo userId ownUserId 字符串数组参数   只修改指定用户之间的对话框的最后一条用于显示的聊天信息
     * @param strings 参数数组
     */
    public void inserJustLastInfo(String... strings)
    {
        new InfoRepository.InsertJustLastInfoAsyncTask(infoDao).execute(strings);
    }


    //内部类，用于在子线程中访问数据库，如果有多个操作的话，每一个操作都要对应一个AsyncTask
    static class InsertMasterAsyncTask extends AsyncTask<Info,Void,Void> {

        private InfoDao infoDao;

        public InsertMasterAsyncTask(InfoDao infoDao) {
            this.infoDao = infoDao;
        }

        @Override
        protected Void doInBackground(Info... infos) {
            infoDao.insertInfo(infos);
            return null;
        }
    }
    //内部类，用于在子线程中访问数据库，如果有多个操作的话，每一个操作都要对应一个AsyncTask
    static class InsertJustLastInfoAsyncTask extends AsyncTask<String,Void,Void> {

        private InfoDao infoDao;

        public InsertJustLastInfoAsyncTask(InfoDao infoDao) {
            this.infoDao = infoDao;
        }

        @Override
        protected Void doInBackground(String... strings) {
            String lastInfo = strings[0];
            String userId = strings[1];
            String ownUserId = strings[2];
            infoDao.insertJustLatInfo(lastInfo,userId,ownUserId);
            return null;
        }
    }


//    public boolean isExistInfo(String userId,String ownerUserId){
//        Info info = infoDao.getInfoCur(userId,ownerUserId);
//        return info != null;
//    }
//
//    //内部类，用于在子线程中访问数据库，如果有多个操作的话，每一个操作都要对应一个AsyncTask
//    static class isExistInfoAsyncTask extends AsyncTask<String,Void,Boolean> {
//
//        private InfoDao infoDao;
//
//        public isExistInfoAsyncTask(InfoDao infoDao) {
//            this.infoDao = infoDao;
//        }
//
//        @Override
//        protected Boolean doInBackground(String... strings) {
//            String userId = strings[0];
//            String ownerUserId = strings[1];
//            Info info =infoDao.getInfoCur(userId,ownerUserId);
//            return info!=null;
//        }
//
//        @Override
//        protected void onPostExecute(Boolean aBoolean) {
//            super.onPostExecute(aBoolean);
//        }
//    }
}
